package com.cxyd.modules.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cxyd.common.constant.Constant;
import com.cxyd.common.page.PageData;
import com.cxyd.common.service.impl.CrudServiceImpl;
import com.cxyd.common.utils.ConvertUtils;
import com.cxyd.modules.sys.dto.SysUserDTO;
import com.cxyd.modules.sys.service.SysUserService;
import com.cxyd.modules.wms.dao.PostWcsDao;
import com.cxyd.modules.wms.dto.ContainerDTO;
import com.cxyd.modules.wms.dto.PostWcsDTO;
import com.cxyd.modules.wms.entity.ContainerEntity;
import com.cxyd.modules.wms.entity.PostWcsEntity;
import com.cxyd.modules.wms.service.PostWcsService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 设备任务
 *
 * @author g_Cycle gengruyi01@163.com
 * @since 1.0.0 2023-04-27
 */
@Service
public class PostWcsServiceImpl extends CrudServiceImpl<PostWcsDao, PostWcsEntity, PostWcsDTO> implements PostWcsService {
    private final SysUserService userService;

    public PostWcsServiceImpl(SysUserService userService) {
        this.userService = userService;
    }

    @Override
    public PageData<PostWcsDTO> page(Map<String, Object> params) {
        //1.查询容器信息
        final IPage<PostWcsEntity> page = baseDao.selectPage(
                getPage(params, "post_id", true),
                getWrapper(params)
        );

        return getPageData(page, PostWcsDTO.class);
    }

    @Override
    public QueryWrapper<PostWcsEntity> getWrapper(Map<String, Object> params) {
        String contNo = (String) params.get("contNo");

        QueryWrapper<PostWcsEntity> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(contNo), "cont_no", contNo);
        return wrapper;
    }


    @Override
    public void updateByPk(Map<String, Object> params) {
        String requestPk = (String) params.get("pk");
        String code = (String) params.get("code");
        String msg = (String) params.get("msg");
        final UpdateWrapper<PostWcsEntity> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("request_pk", requestPk);
        updateWrapper.set("trk_st", ("0".equals(code)) ? 1 : 9);
        updateWrapper.set("ack_code", code);
        updateWrapper.set("ack_msg", msg);
        updateWrapper.set("updater", Constant.ClientCode.WMS.getId());
        updateWrapper.set("update_date", new Date());
        baseDao.update(null, updateWrapper);
    }

    @Override
    public PostWcsDTO getOneRecord() {
        PostWcsEntity entity = baseDao.getOneRecord();
        return ConvertUtils.sourceToTarget(entity, PostWcsDTO.class);
    }
}